﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Forms;
using DataAccess;
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.Native;

namespace QuanLyKhachSan
{
    public partial class FormTimDichVu : XtraForm,ITimKiem<DichVu>
    {
        public FormTimDichVu()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormTimDichVu_Load(object sender, EventArgs e)
        {
            cbbLoaiTim.SelectedIndex = 0;
        }

        private TimDichVuTheo FindBy()
        {
            String findType = cbbLoaiTim.Text;
            switch (findType)
            {
                case "Tìm theo giá":
                    return TimDichVuTheo.TimTheoGia;
                case "Tìm theo mã":
                    return TimDichVuTheo.TimTheoMaDV;
                default:
                    return TimDichVuTheo.TimTheoTen;
            }
        }

        private IEnumerable<Exception> CheckValidate(TimDichVuTheo loaiTim, String value)
        {
            var exceptions = new List<Exception>();
            switch (loaiTim)
            {
                case TimDichVuTheo.TimTheoGia:
                    try
                    {
                        double.Parse(value);
                    }
                    catch (Exception)
                    {
                        exceptions.Add(new Exception("Giá phải là ký số"));
                    }
                    break;
                case TimDichVuTheo.TimTheoMaDV:
                    try
                    {
                        int.Parse(value);
                    }
                    catch (Exception)
                    {
                        exceptions.Add(new Exception("Mã dịch vụ phải là kiểu số nguyên"));
                    }
                    break;
                default:
                    if (value.IsEmpty())
                        exceptions.Add(new Exception("Vui lòng nhập tên cần tìm"));
                    break;
            }
            return exceptions;
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            var findType = FindBy();
            String content = txtNoidungtim.Text;
            IEnumerable<Exception> errors = CheckValidate(findType, content);
            var exceptions = errors as Exception[] ?? errors.ToArray();
            if (!exceptions.Any())
            {
                Expression<Func<DichVu, bool>> newFilter;
                switch (findType)
                {
                    case TimDichVuTheo.TimTheoGia:
                        decimal vl = Convert.ToDecimal(content);
                        newFilter = dv => dv.Gia.Equals(vl);
                        break;
                    case TimDichVuTheo.TimTheoMaDV:
                        int ma = int.Parse(content);
                        newFilter = dv => dv.MaDV.Equals(ma);
                        break;
                    default:
                        newFilter = dv => dv.TenDichVu.Contains(content);
                        break;}var list = new DichVuManager().Find(newFilter);
                if (!list.Sucess())
                {
                    XtraMessageBox.Show("Không tìm thấy");
                }
                else
                {
                    Result = list.Result;
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                //hien thi loi nhap sai noi dung
                lblInfo.Text = exceptions.ToList()[0].ToString();
            }

        }

        public IEnumerable<DichVu> Result { get; set; }

        private void txtNoidungtim_TextChanged(object sender, EventArgs e)
        {
            lblInfo.Text = "";
        }

        private void txtNoidungtim_TextChanged_1(object sender, EventArgs e)
        {
            if(lblInfo.Text.Any())
                lblInfo.Text = "";
        }
    }
}
